package tp.view;

import tp.controller.Controlador;
import tp.model.Ladrillo;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.util.Observable;
import java.util.Observer;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;

/**
 * Esta es la clase que representa a la vista del misil
 * 
 * @author Dr Von Nostrum
 * 
 */
public class LadrilloView extends Component implements Observer {

	private Graphics g;

	public Ladrillo ladrillo;

	public BufferedImage ladrilloImage;

	protected String[] spriteNames;

	protected int currentFrame;

	private int height;

	private int width;
	
	private String nombreArchivo;

	public LadrilloView(Ladrillo unLad, String fileName) {
		unLad.addObserver(this);
		nombreArchivo = fileName;
		ladrillo = unLad;
		g = this.getGraphics();
		setSpriteNames(new String[] { fileName});
		currentFrame = 0;
	}

	private void checkOffscreenImage() {
		Dimension d = CopyOfBoardView1.panel.getSize();// Dimension d =
														// getSize();
		InputStream in = getClass().getResourceAsStream(nombreArchivo);
		JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
		try {
			ladrilloImage = decoder.decodeAsBufferedImage();
			in.close();
		} catch (Exception ex) {

		}

	}

	public void paint(Graphics g) {
		if (ladrillo != null) {
			// Dimension d = this.getParent().getSize();
			Dimension d = CopyOfBoardView1.panel.getSize();
			// checkOffscreenImage();
			// Graphics offG = naveImage.getGraphics();
			// offG.setColor(Color.GRAY);// getBackground());
			// offG.drawImage(naveImage, 0, 0, d.width, d.height, null);
			// offG.fillRect(0, 0, d.width, d.height);

			// Dibujo la imagen propieamente dicha.
			// paintOffscreen(lVerdeImage.getGraphics());

			// Renderizo la imagen finalmente en la pantalla.
			if (ladrillo != null) {
				if ((ladrillo .isVivo() == true) && (ladrillo != null)) {
					g.drawImage(Controlador.spriteCache
							.getSprite(spriteNames[currentFrame]), ladrillo 
							.getMiPosicion().getXInt(), d.height
							- ladrillo .getMiPosicion().getYInt()
							, (int) ladrillo .getMiAncho(), (int) ladrillo.getMiAlto(), null);
				}
			}
		}
	}

	/*
	 * public void paintOffscreen(Graphics g) { //Dimension d =
	 * this.getParent().getSize(); if (lVerde!=null){ Dimension d =
	 * CopyOfBoardView1.panel.getSize(); if ((lVerde.isVivo() ==
	 * true)&&(lVerde!=null)){
	 * g.drawImage(lVerdeImage,lVerde.getMiPosicion().getXInt() - (int)
	 * lVerde.getMiAncho()/2, d.height - lVerde.getMiPosicion().getYInt() -
	 * (int)lVerde.getMiAlto()/2,
	 * (int)lVerde.getMiAncho(),(int)lVerde.getMiAlto(),null); } } }
	 */
	public void update(Observable unLadAzul, Object arg1) {
		this.repaint();
		/*
		 * if (nave!=null){ if (nave.getMiPosicion()!=null){ g =
		 * this.getGraphics(); Dimension d = this.getParent().getSize();
		 * g.setColor(Color.BLUE); g.fillOval(nave.getMiPosicion().getXInt() -
		 * (int) nave.getMiRadio(), d.height - nave.getMiPosicion().getYInt() -
		 * (int) nave.getMiRadio(), (int) nave.getMiRadio() * 2, (int)
		 * nave.getMiRadio() * 2); }else{ nave=null; } }
		 */
	}

	public void setSpriteNames(String[] names) {
		spriteNames = names;
		height = 0;
		width = 0;
		for (int i = 0; i < names.length; i++) {
			BufferedImage image = Controlador.spriteCache
					.getSprite(spriteNames[i]);
			height = Math.max(height, image.getHeight());
			width = Math.max(width, image.getWidth());
		}
	}

	public void Destruir() {
		this.ladrillo = null;
	}
}
